package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.system.SystemOpenInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SystemOpenDaoImpl extends TempDao {

	public Map<Integer, SystemOpenInfo> getSystemOpenInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_system_open order by id";
		Map<Integer, SystemOpenInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				SystemOpenInfo data = new SystemOpenInfo();
				data.setSystemId(rs.getInt("id"));
				data.setConditionType(rs.getInt("conditionType"));
				data.setValue(new BigInteger(rs.getString("value")));
				data.setReward(PropertyHelper.parseStringToProperty(rs.getString("reward")));
				String showReward = rs.getString("showReward");
				if (!StringUtils.isNullOrEmpty(showReward)) {
					data.setShowReward(PropertyHelper.parseStringToProperty(showReward));
				}
				dataMap.put(data.getSystemId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
